﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web.Configuration;
using Bots.Configuration;

namespace Bots.Index
{
    static public class IncIndexManager
    {
         //Initialization related variables and logic
        private static bool isInitialized = false;
        private static Exception initializationException;
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(IncIndexManager));
        private static object initializationLock = new object();
        private static IncIndexProvider defaultProvider;
        private static IncIndexCollection providerCollection;

        public static IncIndexProvider Provider
        {
            get
            {
                return defaultProvider;
            }
        }

        public static IncIndexCollection Providers
        {
            get
            {
                return providerCollection;
            }
        }

        static IncIndexManager()
        {
            Initialize();
        }

        private static void Initialize()
        {

            try
            {
                //Get the feature's configuration info
                IncIndexSection qc = (IncIndexSection)ConfigurationManager.GetSection("IncIndexProviders");

                if (qc.DefaultProvider == null || qc.Providers == null || qc.Providers.Count < 1)
                    throw new ApplicationException("You must specify a valid default provider.");

                //Instantiate the providers
                providerCollection = new IncIndexCollection();
                ProvidersHelper.InstantiateProviders(qc.Providers, providerCollection, typeof(IncIndexProvider));
                providerCollection.SetReadOnly();
                defaultProvider = providerCollection[qc.DefaultProvider];
                if (defaultProvider == null)
                {
                    throw new ConfigurationErrorsException(
                        "You must specify a default provider for the feature.",
                        qc.ElementInformation.Properties["defaultProvider"].Source,
                        qc.ElementInformation.Properties["defaultProvider"].LineNumber);
                }
            }
            catch (Exception ex)
            {
                logger.Fatal(ex);
                initializationException = ex;
                isInitialized = true;
                throw ex;
            }

            isInitialized = true; 
        }

        /// <summary>
        /// Add Incremental index object to repository
        /// </summary>
        /// <param name="domainObject"></param>
        static public void IncIndexAdd(DomainObject domainObject) {
            defaultProvider.IncIndexAdd(domainObject);
        }

        /// <summary>
        /// Add Incremental index objects to repository
        /// </summary>
        /// <param name="domainObjects"></param>
        static public void IncIndexAdd(DomainObject[] domainObjects){
            defaultProvider.IncIndexAdd(domainObjects);
        }

        /// <summary>
        /// Fetch domainObjects from repository
        /// </summary>
        /// <param name="maxCount">max count every time</param>
        /// <returns></returns>
        static public List<DomainObject> FetchIncIndicex(int maxCount){
            return defaultProvider.FetchIncIndicex(maxCount);
        }

    }
}
